package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.TeaShopWorkPlace;
import com.ruoyi.system.domain.WarehouseDeliveryInformation;
import com.ruoyi.system.domain.WarehouseInventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WarehouseMapper;
import com.ruoyi.system.domain.Warehouse;
import com.ruoyi.system.service.IWarehouseService;
import com.ruoyi.common.core.text.Convert;

/**
 * 原料仓库信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-31
 */
@Service
public class WarehouseServiceImpl implements IWarehouseService 
{
    @Autowired
    private WarehouseMapper warehouseMapper;

    /**
     * 查询原料仓库信息
     * 
     * @param id 原料仓库信息主键
     * @return 原料仓库信息
     */
    @Override
    public Warehouse selectWarehouseById(Long id)
    {
        return warehouseMapper.selectWarehouseById(id);
    }

    /**
     * 查询原料仓库信息列表
     * 
     * @param warehouse 原料仓库信息
     * @return 原料仓库信息
     */
    @Override
    public List<Warehouse> selectWarehouseList(Warehouse warehouse)
    {
        return warehouseMapper.selectWarehouseList(warehouse);
    }

    /**
     * 新增原料仓库信息
     * 
     * @param warehouse 原料仓库信息
     * @return 结果
     */
    @Override
    public int insertWarehouse(Warehouse warehouse)
    {
        return warehouseMapper.insertWarehouse(warehouse);
    }

    /**
     * 修改原料仓库信息
     * 
     * @param warehouse 原料仓库信息
     * @return 结果
     */
    @Override
    public int updateWarehouse(Warehouse warehouse)
    {
        return warehouseMapper.updateWarehouse(warehouse);
    }

    /**
     * 批量删除原料仓库信息
     * 
     * @param ids 需要删除的原料仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseByIds(String ids)
    {
        return warehouseMapper.deleteWarehouseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除原料仓库信息信息
     * 
     * @param id 原料仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseById(Long id)
    {
        return warehouseMapper.deleteWarehouseById(id);
    }

    @Override
    public List<WarehouseInventory> selectInventoryList(WarehouseInventory warehouseInventory){
        return warehouseMapper.selectInventoryList(warehouseInventory);
    }
    @Override
    public int insertDeliverMaterial(Long warehouseId, Long teaShopId, String materialIds, String deliverAmounts) {
        Long[] materials = Convert.toLongArray(materialIds);
        Long[] delivers = Convert.toLongArray(deliverAmounts);
        List<WarehouseDeliveryInformation> list = new ArrayList<>();
        List<WarehouseDeliveryInformation> ids = new ArrayList<>();
        for (int i = 0; i < materials.length; i++) {
            WarehouseDeliveryInformation temp = new WarehouseDeliveryInformation();
            temp.setWarehouseId(warehouseId);
            temp.setTeaShopId(teaShopId);
            temp.setRawMaterialId(materials[i]);
            temp.setAmount(delivers[i]);
            temp.setState("1");
            Long id = warehouseMapper.selectSubtractInventory(temp);
            temp.setInventoryId(id);
            list.add(temp);
            int b = warehouseMapper.updateSubtractInventory(temp);
        }
        return warehouseMapper.insertDeliverMaterial(list);
    }

}
